// In the name of Allah
// Khodaya khodet komak kon
// Hope is last to die
// Let's go
// Hello today I decided to solve at least 5 great problems per day for 5 months
// 2023/11/24
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
#define pb push_back
#define S second
#define F first
#define mp make_pair
#define smax(x, y) (x) = max((x), (y))
#define smin(x, y) (x) = min((x), (y))
#define all(x) (x).begin(), (x).end()
#define set_dec(x) cout << fixed << setprecision(x);
#define kill(x) cout << x << endl, exit(0)
#define sz(x) ((int)(x).size())
#define file_io freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
const ll maxn = 4e5 + 100, lg = 21, mod = 998244353;
const ll inf = 1e15 + 7;
ll n, m, dist2[maxn * (lg + 1)];
pll dist1[maxn];
vector<ll> adj[maxn];
vector<pair<ll, pll>> from[maxn];
vector<pll> fr[maxn * (lg + 1)];
ll tavan(ll a, ll b){
if(!b) return 1;
ll x = tavan(a, b / 2);
x = (x * x) % mod;
if(b % 2) x = (x * a) % mod;
return x;
}
void dij1(){
memset(dist1, 63, sizeof dist1);
dist1[0] = mp(0, 0);
set<pair<pll, ll>> st;
st.insert(mp(mp(0, 0), 0));
while(sz(st)){
ll v = (*st.begin()).S;
st.erase(st.begin());
for(auto e: from[v]){
pll p = dist1[v];
p.F += e.S.F;
p.S += e.S.S;
if(p < dist1[e.F]){
st.erase(mp(dist1[e.F], e.F));
dist1[e.F] = p;
st.insert(mp(dist1[e.F], e.F));
}
}
}
// cout << dist1[n - 1].F << ' ' << dist1[n - 1].S << '\n';
// cout << dist1[2 * n - 1].F << ' ' << dist1[2 * n - 1].S << '\n';
}
void dij2(){
memset(dist2, 63, sizeof dist2);
set<pll> st;
st.insert(mp(0, 0));
while(sz(st)){
ll v = (*st.begin()).S, d = (*st.begin()).F;
st.erase(st.begin());
if(dist2[v] <= inf) continue;
dist2[v] = d;
for(auto [u, w]: fr[v]){
if(dist2[u] > d + w){
st.insert(mp(d + w, u));
}
}
}
// for(int i = 0; i <= lg; i++) cout << dist2[i * n + n - 1] << '\n';
}
int32_t main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> m;
for(int i = 0; i < m; i++){
ll u, v; cin >> u >> v;
adj[--u].pb(--v);
}
for(int i = 0; i < n; i++){
from[i].pb(mp(i + n, mp(1, 0)));
from[i + n].pb(mp(i, mp(1, 0)));
for(auto u: adj[i]){
from[i].pb(mp(u, mp(0, 1)));
from[u + n].pb(mp(i + n, mp(0, 1)));
}
}
dij1();
ll t1 = dist1[n - 1].F, r1 = dist1[n - 1].S;
ll t2 = dist1[2 * n - 1].F, r2 = dist1[2 * n - 1].S;
if(min(t1, t2) >= 30){
if(t1 == t2){
cout << (tavan(2, t1) - 1 + min(r1, r2)) % mod;
}
else if(t1 < t2){
cout << (tavan(2, t1) - 1 + r1) % mod;
}
else cout << (tavan(2, t2) - 1 + r2) % mod;
return 0;
}
for(int b = 0; b <= lg; b++){
for(int i = 0; i < n; i++){
for(auto u: adj[i]){
if(b % 2 == 0) fr[b * n + i].pb(mp(b * n + u, 1));
else fr[b * n + u].pb(mp(b * n + i, 1));
}
}
if(b != lg){
for(int i = 0; i < n; i++){
fr[b * n + i].pb(mp((b + 1) * n + i, (1ll << b)));
}
}
}
dij2();
ll ans = inf;
for(int i = 0; i <= lg; i++) smin(ans, dist2[i * n + n - 1]);
smin(ans, (1ll << dist1[n - 1].F) + dist1[n - 1].S);
smin(ans, (1ll << dist1[2 * n - 1].F) + dist1[2 * n - 1].S);
cout << (ans % mod);
return 0;
}
1609C - Complex Market Analysis | 1657E - Star MST |
1143B - Nirvana | 1285A - Mezo Playing Zoma |
919B - Perfect Number | 894A - QAQ |
1551A - Polycarp and Coins | 313A - Ilya and Bank Account |
1469A - Regular Bracket Sequence | 919C - Seat Arrangements |
1634A - Reverse and Concatenate | 1619C - Wrong Addition |
1437A - Marketing Scheme | 1473B - String LCM |
1374A - Required Remainder | 1265E - Beautiful Mirrors |
1296A - Array with Odd Sum | 1385A - Three Pairwise Maximums |
911A - Nearest Minimums | 102B - Sum of Digits |
707A - Brain's Photos | 1331B - Limericks |
305B - Continued Fractions | 1165B - Polycarp Training |
1646C - Factorials and Powers of Two | 596A - Wilbur and Swimming Pool |
1462B - Last Year's Substring | 1608B - Build the Permutation |
1505A - Is it rated - 2 | 169A - Chores |